package com.lx.boot.db;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lx.annotation.Note;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

public interface MpMapper<T> extends BaseMapper<T> {

    @Note("物理删除")
    int deleteAbsolute(@Param("ew") Wrapper<T> queryWrapper);

    @Note("物理删除")
    int deleteAbsoluteById(Serializable id);

    @Note("物理删除")
    int deleteAbsoluteBatchIds(@Param("coll") Collection<?> idList);

    @Note("获取实体类型")
    default Class<T> currentModelClass() {
        return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), MpMapper.class, 0);
    }

    @Note("批量插入")
    default boolean insertBatch(Collection<T> entityList) {
        return Db.saveBatch(entityList);
    }

    @Note("批量更新")
    default boolean updateBatchById(Collection<T> entityList) {
        return Db.updateBatchById(entityList);
    }

    /**
     * 批量插入或更新
     */
    @Note("批量更新")
    default boolean insertOrUpdateBatch(Collection<T> entityList) {
        return Db.saveOrUpdateBatch(entityList);
    }

    /**
     * 批量插入(包含限制条数)
     */
    @Note("批量更新")
    default boolean insertBatch(Collection<T> entityList, int batchSize) {
        return Db.saveBatch(entityList, batchSize);
    }

    /**
     * 批量更新(包含限制条数)
     */
    @Note("批量更新")
    default boolean updateBatchById(Collection<T> entityList, int batchSize) {
        return Db.updateBatchById(entityList, batchSize);
    }

    /**
     * 批量插入或更新(包含限制条数)
     */
    @Note("批量更新")
    default boolean insertOrUpdateBatch(Collection<T> entityList, int batchSize) {
        return Db.saveOrUpdateBatch(entityList, batchSize);
    }

    /**
     * 插入或更新(包含限制条数)
     */
    @Note("批量更新")
    default boolean insertOrUpdate(T entity) {
        return Db.saveOrUpdate(entity);
    }

    @Note("查询并转换")
    default <C> List<C> selectObjs(Wrapper<T> wrapper, Function<? super Object, C> mapper) {
        return this.selectObjs(wrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
    }
 
}